package com.wanmait.baohan.controller.logisticsManagement;


import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.*;
import com.wanmait.baohan.vo.DataVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * TODO
 *
 * @author 打豆豆
 * @version 1.0
 * @date 2020/12/16 16:43
 * @description: 预定酒店控制器
 */
@Controller
@RequestMapping("/management")
public class HotelManagementController {
    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private TravelApplicationService travelApplicationService;

    @Resource
    private TravelPeopleService travelPeopleService;

    @Resource
    private BookingHotelService bookingHotelService;

    @Resource
    private ProvincesCityService provincesCityService;

    @Resource
    private HotelApprovalService hotelApprovalService;

    /**
     * hxn
     * @param model
     * @return
     * 打开预订酒店申请页面
     */
    @RequestMapping("/bookingHotelApplication")
    public String bookingHotelApplication(Integer id, Model model){
        model.addAttribute("allProvince", provincesCityService.findAllProvince());
        model.addAttribute("applicationBasicInfo", travelApplicationService.findById(id));
        model.addAttribute("applicationMembers",travelPeopleService.findAllAllpicationStaffs(id));
        return "/management/bookingHotelApplication";
    }
    @RequestMapping("applicationsCanBookHotel")
    public String applicationsCanBookHotel(){
        return "management/applicationsCanBookHotel";
    }
    /**
     * hxn
     * @param id
     * @param request
     * @return
     * 得到所有选中项目类型的可以申请的项目
     */
    @RequestMapping("/getHotelApplicationsByProjectType")
    @ResponseBody
    public List getHotelApplicationsByProjectType(Integer id, HttpServletRequest request){
        List applications = null;
        Staff staff = (Staff) request.getSession().getAttribute("user");
        Integer userId = staff.getId();
        switch (id){
            case 1:
                applications = travelApplicationService.findAllApplicationCanBooking(userId);
                break;
            case 2:
                //applications = ;
        }
        return applications;
    }

    /**
     * hxn
     * @param id
     * @return
     * 得到项目中所有的人
     */
    @RequestMapping("getHotelApplicationStaffs")
    @ResponseBody
    public List<TravelPeople> getHotelApplicationStaffs(Integer id){
        return travelPeopleService.findAllAllpicationStaffs(id);
    }

    /**
     * hxn
     * @param bookingHotelApplication
     * 提交申请
     */
    @RequestMapping("submitHotelApplication")
    @ResponseBody
    public void submitHotelApplication(BookingHotelApplication bookingHotelApplication){
        bookingHotelService.insert(bookingHotelApplication);
    }

    /**
     * hxn
     * @param code
     * @return
     * 通过省获取市
     */
    @RequestMapping("getHotelCity")
    @ResponseBody
    public List<ProvincesCity> getHotelCity(String code){
        return provincesCityService.findCityByProvinceCode(code);
    }

    /**
     * 通过市获取县
     * @param code
     * @return
     */
    @RequestMapping("getHotelDistrict")
    @ResponseBody
    public List<ProvincesCity> getHotelDistrict(String code){
        return provincesCityService.findDistrictByCityCode(code);
    }

    /**
     * hxn
     * @param model
     * @return
     * 打开我的申请页面
     */
    @RequestMapping("table-HotelApplications")
    public String tebleHotelApplciations(Model model){
        return "management/table-HotelApplications";
    }

    /**
     * hxn
     * @param page
     * @param limit
     * @param request
     * @return
     * 获取我的订酒店申请
     */
    @RequestMapping("getMyHotelApplications")
    @ResponseBody
    public DataVo<BookingHotelApplication> getMyHotelApplications(Integer page, Integer limit, HttpServletRequest request){
        //创建查询条件对象
        BookingHotelApplication bookingHotelApplication = new BookingHotelApplication();
        Staff staff = (Staff) request.getSession().getAttribute("user");
        bookingHotelApplication.setStaff(staff);


        DataVo<BookingHotelApplication> dataVo = new DataVo();
        PageInfo<BookingHotelApplication> pageInfo = bookingHotelService.selectMyHotelApplications(page, limit, bookingHotelApplication);
        dataVo.setCount(pageInfo.getTotal());
        dataVo.setData(pageInfo.getList());
        return dataVo;
    }

    /**
     * hxn
     * @param id
     * @param model
     * @return
     * 获得酒店申请详情
     */
    @RequestMapping("/getHotelApplicationDetails")
    public String getHotelApplicationDetails(Integer id, Model model){
        BookingHotelApplication bookingHotelApplication = bookingHotelService.findById(id);
        model.addAttribute("bookHotelApplication", bookingHotelApplication);
        model.addAttribute("travelPeoples", travelPeopleService.findAllAllpicationStaffs(bookingHotelApplication.getApplicationId()));
        model.addAttribute("approvalLog", hotelApprovalService.findById(id));
        return "management/hotelApplicationDetails";
    }

    /**
     * hxn
     * @return
     * 打开所有申请页面
     * */
    @RequestMapping("/allHotelApplicationsList")
    public String getAllHotelApplications(){
        return "management/allHotelApplicationsList";
    }

    /**
     * hxn
     * @param page
     * @param limit
     * @param bookingHotelApplication
     * @return
     * 获取所有的酒店申请，有搜索条件
     */
    @RequestMapping("/getAllHotelApplications")
    @ResponseBody
    public DataVo<BookingHotelApplication> getAllHotelApplications(Integer page, Integer limit, BookingHotelApplication bookingHotelApplication){
        DataVo<BookingHotelApplication> dataVo = new DataVo<>();
        PageInfo<BookingHotelApplication> bookingHotelApplications = bookingHotelService.findAllWithPageAndLimit(page, limit, bookingHotelApplication);
        dataVo.setData(bookingHotelApplications.getList());
        dataVo.setCount(bookingHotelApplications.getTotal());
        return dataVo;
    }

    @RequestMapping("saveHotelApprovalForm")
    @ResponseBody
    public void saveHotelApprovalForm(Integer id, String value, Integer state){

        HotelApproval hotelApproval = new HotelApproval();
        hotelApproval.setApprovalInfo(value);
        hotelApproval.setState(state);
        hotelApproval.setBookingHotelApplicationId(id);
        bookingHotelService.submitApprove(hotelApproval);
    }

    @RequestMapping("hotelLedger")
    public  void hotelLedger(Integer id,Model model)
    {
       List<HotelAccount>list = bookingHotelService.findBookingListById(id);
       model.addAttribute("list",list);
    }

}
